<?php

namespace Dddxiu\rules;

use Dddxiu\Layer;

/**
 * 多选
 */
class Multi implements Rule
{
    // flag
    const F = 'multi';


    /**
     * 多选
     * @param array $input
     * @param string $field
     * @param Layer $layer
     * @param array $args
     * @return \Closure|bool
     * @throws \Exception
     */
    public static function valid($input, $field, $layer, $args)
    {
        $vars = $input[$field];
        if (is_string($vars)) {
            $vars = explode(',', $vars);
        }

        if (!is_array($vars)) {
            return false;
        }

        foreach ($vars as $var) {
            if (!in_array($var, $args)) {
                return false;
            }
        }
        return $layer::next();
    }
}
